<?php
/**
 * Banners_model Class
 * @package TIAP
 * @subpackage Model
 * @category banners
 * @author Suraj Maharjan
 * @link http://twitter.com/
 */
class Banners_model extends MY_Model{
	var $your_info_table;

	function Banners_model(){
		parent::MY_Model();
	
		$this->banners_table = 'tbl_banners';
	   $this->stats_table = 'tbl_banners_stats';
       $this->banners_default_table = 'tbl_banners_default';
	}
    
    function getBanners($ban_id) {
             $sql = "select * from `$this->banners_table`  ";
            $sql .= " WHERE ban_id = '$ban_id' "; 
            $query = $this->db->query($sql);
		    if($query->num_rows() > 0)
		{
		  return $query->row_array();
         		} else {
		  return 0;
		}
      }
      function getDefaultBanners($ban_id) {
             $sql = "select * from `$this->banners_default_table`  ";
            $sql .= " WHERE ban_id = '$ban_id' "; 
            $query = $this->db->query($sql);
		    if($query->num_rows() > 0)
		{
		  return $query->row_array();
         		} else {
		  return 0;
		}
      }
	
       function activeBannersCount()
	   {
		   $sql = "select * from `$this->banners_table`  ";
           $sql .= " WHERE ban_status = '1' "; 
               
           $query = $this->db->query($sql);
		    if($query->num_rows() > 0)
		  {
			  return $query->num_rows();
		  } else {
			  return 0;
		  }
	   }
       
        function activeBanners($limit='') {
             $sql = "select * from `$this->banners_table`  ";
           $sql .= " WHERE ban_status = '1' "; 
           $sql.=" ORDER BY ban_date_from ASC  ".$limit;
		   
            $query = $this->db->query($sql);
			
                  if($query->num_rows() > 0)
		{
		  return $query->result_array();
         		} else {
		  return 0;
		}
      }
      
       function displayBanners($placement='') {
             $sql = "select * from `$this->banners_table`  ";
           $sql.= " WHERE ban_status = '1' "; 
           $sql.= " AND ban_placement = '$placement' ";
           $sql.=" ORDER BY RAND()   ";
		   
            $query = $this->db->query($sql);
			
                  if($query->num_rows() > 0)
		{
		  return $query->result_array();
         		} else {
		  return false;
		}
       
      }
      
      function displayBannersDefault($placement='') {
             $sql = "select * from `$this->banners_default_table`  ";
           $sql.= " WHERE ban_status = '1' "; 
           $sql.= " AND ban_placement = '$placement' ";
          // $sql.=" ORDER BY RAND()  LIMIT 1 ";
		   
            $query = $this->db->query($sql);
			
                  if($query->num_rows() > 0)
		{
		  return $query->row_array();
         		} else {
		  return false;
		}
       
      }
      
      function inactiveBannersCount()
	   {
		   $sql = "select * from `$this->banners_table`  ";
           $sql .= " WHERE ban_status != '1' "; 
               
           $query = $this->db->query($sql);
		    if($query->num_rows() > 0)
		  {
			  return $query->num_rows();
		  } else {
			  return 0;
		  }
	   }
       
        function inactiveBanners($limit='') {
             $sql = "select * from `$this->banners_table`  ";
           $sql .= " WHERE ban_status != '1' "; 
           $sql.=" ORDER BY ban_date_from ASC  ".$limit;
		   
            $query = $this->db->query($sql);
			
                  if($query->num_rows() > 0)
		{
		  return $query->result_array();
         		} else {
		  return 0;
		}
      }
      
    
        function defaultBanners($limit='') {
             $sql = "select * from `$this->banners_default_table`  ";
           $sql .= " WHERE ban_status = '1' "; 
          
            $query = $this->db->query($sql);
			
                  if($query->num_rows() > 0)
		{
		  return $query->result_array();
         		} else {
		  return 0;
		}
      
      }
      
      
      
       function insertBannersData($data)
    {
      $this->db->set($data);
      $this->db->insert($this->banners_table);
        if($new_ban_id = $this->db->insert_id()) 
     {return $new_ban_id;} 
        
    }
     function updateBannersData($ban_id,$data){
            $this->db->where('ban_id',$ban_id);
            $this->db->update($this->banners_table,$data);
                   
        }
     function updateBannersDefault($ban_id,$data){
            $this->db->where('ban_id',$ban_id);
            $this->db->update($this->banners_default_table,$data);
                  
        }
     function deleteBanners($ban_id){
      
          $this->db->where('ban_id',$ban_id);
	      $this->db->delete($this->banners_table);
        
     }
        
     function detailBanners($ban_id){
             $sql = "select * from `$this->banners_table` where `ban_id` = '$ban_id'  ";
            $query = $this->db->query($sql);
            if($query->num_rows() > 0)
        		{
        		  return $query->row_array();
                 		} else {
        		  return 0;
        		}
       }
     /*Admin end*/  
     /*front end*/
  
      function insertBannersClick($ban_id){
      
       $check_session=$this->checkBannersStats($ban_id,'ban_stats_click');
       if(!$check_session)
       {
            $data=array('ban_stats_ban_id' => $ban_id,
                        'ban_stats_date' => date("Y-m-d H:i:s"),
                        'ban_stats_click'  => '1',
                        'ban_stats_ip'     => $_SERVER['REMOTE_ADDR'],
                        'ban_stats_ses_id' => $this->session->userdata('session_id')
            );
           $this->db->set($data);
            $this->db->insert($this->stats_table);
            
        }  
      
        if($new_ban_id = $this->db->insert_id()) 
            {
       
            $ban_sum = $this->getBannersSum($ban_id,'ban_stats_click');
           $data = array('ban_track_click' => $ban_sum);
           $this->db->where('ban_id',$ban_id);
           $this->db->update($this->banners_table,$data);
           
            $ban_impression = $this->getBannersField($ban_id,'ban_impression');
             if($ban_sum >= $ban_impression)
                      {
                        $this->doInactiveBanners($ban_id);
                        $this->deleteBannersStats($ban_id);
                       
                      }
                  
                    return $new_ban_id;
            } 
        }
        
     function checkBannersStats($ban_id,$ban_stats)
     {
        $session_id = $this->session->userdata('session_id');
         $sql = "select * from `$this->stats_table` where `ban_stats_ban_id` = '$ban_id' and `ban_stats_ses_id` = '$session_id' and `$ban_stats` = '1'  ";
            $query = $this->db->query($sql);
            if($query->num_rows() > 0)
        		{
        		  return true;
        	
                 		} else {
        		 
                  return false;
        		}
     }  
     function insertBannersViews($ban_id)
     {
        $check_view = $this->checkBannersStats($ban_id,'ban_stats_view');
        if($check_view)
        {
           return false;
        }
         $data=array('ban_stats_ban_id' => $ban_id,
                        'ban_stats_date' => date("Y-m-d H:i:s"),
                        'ban_stats_view'  => '1',
                        'ban_stats_ip'     => $_SERVER['REMOTE_ADDR'],
                        'ban_stats_ses_id' => $this->session->userdata('session_id')
            );
          
        $this->db->set($data);
      $this->db->insert($this->stats_table);
      
       if($new_ban_id = $this->db->insert_id())
       { 
       $ban_sum = $this->getBannersSum($ban_id,'ban_stats_view');
       $data = array('ban_track_view' => $ban_sum);
       $this->db->where('ban_id',$ban_id);
       $this->db->update($this->banners_table,$data);
   
     $ban_impression = $this->getBannersField($ban_id,'ban_impression');
         if($ban_sum >= $ban_impression)
                  {
                    $this->doInactiveBanners($ban_id);
                   
                    $this->deleteBannersStats($ban_id);
                   
                  }
        return $new_ban_id;
    } 
        
     }
    
      function getBannersSum($ban_id ,$ban_stats)
     {
       $sql = "select SUM($ban_stats) from `$this->stats_table` where `ban_stats_ban_id` = '$ban_id'  ";
            $query = $this->db->query($sql);
            if($query->num_rows() > 0)
        		{
        		  $ban_detail= $query->row_array();
                 
                  return $ban_detail["SUM($ban_stats)"];
                 // }
              	} else {
        		  return false;
        		}
     }
     
     function doInactiveBanners($ban_id)
     {
      
       $data = array('ban_status' => '0','ban_date_to' => date("Y-m-d H:i:s"));
        $this->db->where('ban_id',$ban_id);
        $this->db->update($this->banners_table,$data);
       
     }
     
     function deleteBannersStats($ban_id)
     {
          $this->db->where('ban_stats_ban_id',$ban_id);
	      $this->db->delete($this->stats_table);
     }
        
     function getBannersField($ban_id,$ban_field='')
     {
       $sql = "select `$ban_field` from `$this->banners_table` where `ban_id` = '$ban_id'  ";
            $query = $this->db->query($sql);
            if($query->num_rows() > 0)
        		{
        		  $ban_detail= $query->row_array();
                  return $ban_detail[$ban_field];
                 		} else {
        		  return false;
        		}
     }
     
  
       
}